package org.thanos.demo.service.impl;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thanos.demo.entity.QuartzEntity;
import org.thanos.demo.mapper.JobMapper;
import org.thanos.demo.service.IJobService;

import java.util.List;

@Service("jobService")
public class JobServiceImpl implements IJobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private Scheduler scheduler;

    @Override
    public List<QuartzEntity> listQuartzEntity(String jobName) throws SchedulerException {
        List<QuartzEntity> list = jobMapper.selectListQuartzEntity(jobName);
        for (QuartzEntity quartzEntity : list) {
            JobKey key = new JobKey(quartzEntity.getJobName(), quartzEntity.getJobGroup());
            JobDetail jobDetail = scheduler.getJobDetail(key);
            quartzEntity.setJobMethodName(jobDetail.getJobDataMap().getString("jobMethodName"));
        }
        return list;
    }

    @Override
    public Integer listQuartzEntity() {
        return jobMapper.selectCountQuartzEntity();
    }
}
